<?php


namespace Common\Model;


use Org\Util\Base;
use Think\Model;

class GoodsModel extends Model
{

    protected $pk        = 'gid';//主键
    protected $tableName = 'goods';//指定操作数据表
    protected $_validate = [
        ['gname','require','请输入商品名称'],
//        ['click','require','请输入商品点击次数']
    ];
    //获取分类数据
    public function getCate(){

        return (new Base())->tree(m('category')->select(),'cname');
    }
    //获取品牌数据
    public function getBrand()
    {
        return m('brand')->select();
    }

    public function store($data)
    {
        if (isset($data['gid'])){
            $gid=$data['gid'];
        }else{
            $gid=null;
        }

//        dddagid);die;
        if (!$this->create()){
            return ['vaild'=>0,'mag'=>$this->getError()];
        }
        $detaildModel=new DetailModel();
        if (!$detaildModel->create()){
            return ['vaild'=>0,'msg'=>$detaildModel->getError()];
        }

        if (!isset($data['attr'])||!isset($data['spec'])){
            return ['vaild'=>0,'msg'=>'请选择属性规格数'];
        }
        $data['time'] = time();
        $data['aid'] = 1;//管理员id取自session，这里没有登录写死1
        if($gid){
            $data['gid']=$gid ;
            $deid=$this->save($data);
        }else{
            $gid=$this->add($data);
    }


        $bigPath = '';
        $medium = '';
        $smallPath = '';

//        dd($data['img']);die;
        foreach($data['img'] as $v) {

            //大
            $image = new \Think\Image();
            $image->open($v);
            $big = dirname( $v ) . '/big_' . basename( $v );
            $bigPath .= $big . '|';
            $image->thumb( 800 , 800 )->save( $big );

            //中
            $med = dirname( $v ) . '/medium_' . basename( $v );
            $medium .= $med . '|';
            $image->thumb( 400 , 400 )->save( $med );
            //小
            $sma = dirname( $v ) . '/small_' . basename( $v );
            $smallPath .= $sma . '|';
            $image->thumb( 200 , 200 )->save( $sma );

        }
        $detailData=[
            'small'=>rtrim($medium,'|'),
            'medium'=>rtrim($smallPath,'|'),
            'big'=>rtrim($bigPath,'|'),
            'intro'=>$data['intro'],
            'service'=>$data['service'],
            'gid'=>$gid,

        ];
        if ($deid){

            $detailData['deid']=m('detail')->where("gid=$gid")->getField('deid') ;
            $detaildModel->save($detailData);
        }else{
            $detaildModel->add($detailData);
        }




        $goodsAttrModel=new GoodsAttrModel();
        if ($gid){
            $User = M("goods_attr"); // 实例化User对象
            $User->where("gid=$gid")->delete(); // 删除id为5的用户数据
        }

        foreach($data['attr'] as $k=>$v){
            if($v){
                $goodsAttrDate = [
                    'gtvalue'=>$v,
                    'added'=>0,
                    'taid'=>$k,
                    'gid'=>$gid,
                ];

                    $goodsAttrModel->add($goodsAttrDate);


            }
        }
        foreach($data['spec'] as $k=>$v)
        {
            foreach($v['spec'] as $kk=>$vv){
                $goodsSpecDate = [
                    'gtvalue'=>$vv,
                    'added'=>$v['added'][$kk],
                    'taid'=>$k,
                    'gid'=>$gid,
                ];

                $goodsAttrModel->add($goodsSpecDate);
            }
        }
        if ($gid){
            m('goods_list')->where("gid={$gid}")->delete();
        }

        return ['valid'=>1,'msg'=>'操作成功'];
    }


    public  function del($gid){
        if ($gid){
            // 实例化User对象
            M("goods_attr")->where("gid=$gid")->delete(); // 删除id为5的用户数据
            M("goods")->where("gid=$gid")->delete(); // 删除id为5的用户数据
            M("detail")->where("gid=$gid")->delete(); // 删除id为5的用户数据

        }
        return ['valid'=>1,'msg'=>'操作成功'];

    }


}

